package com.ruoyi.base.utils.inputcheck;

import com.ruoyi.base.enums.ExceptionEnum;
import com.ruoyi.base.exception.SysException;
import com.ruoyi.base.utils.number.NumberUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * 入参检查
 *
 * @author lcf
 */
public class InputCheckUtils {
    //数字
    private static final String REG_NUMBER = ".*\\d+.*";
    //小写字母
    private static final String REG_UPPERCASE = ".*[A-Z]+.*";
    //大写字母
    private static final String REG_LOWERCASE = ".*[a-z]+.*";
    //手机号码
    private static Pattern phonePattern = Pattern.compile("^[1]\\d{10}$");
    /**
     * 检查是否包含中文
     */
    private static Pattern chinese = Pattern.compile("[\u4e00-\u9fa5]");

    /**
     * 检查密码是否合法
     */
    public static void checkPwd(String pwd) {
        if (StringUtils.isEmpty(pwd)) {
            throw new SysException(ExceptionEnum.UN_LEGAL_PWD);
        }
        if (pwd.length() < 8 || pwd.length() > 16) {
            throw new SysException(ExceptionEnum.UN_LEGAL_PWD);
        }

        int i = 0;
        if (pwd.matches(REG_NUMBER)) {
            i++;
        }
        if (pwd.matches(REG_LOWERCASE)) {
            i++;
        }
        if (pwd.matches(REG_UPPERCASE)) {
            i++;
        }

        if (i  < 3 ) {
            throw new SysException(ExceptionEnum.UN_LEGAL_PWD);
        }
    }

    /**
     * 判断字符串中是否包含中文
     * @param str
     * 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        return chinese.matcher(str).find();
    }

    /**
     * 检查账号合法性
     */
    public static void checkAccount(String account) {
        if (account.length() >= 32) {
            throw new SysException("账号位数过长");
        }

        checkChineseWord(account);
    }
    /**
     * 检查手机号码合法性
     */
    public static void checkPhone(String phone) {
        if (phone.length() != 11) {
            throw new SysException(ExceptionEnum.PHONE_ERROR);
        }

        if (!phonePattern.matcher(phone).matches()) {
            throw new SysException(ExceptionEnum.PHONE_ERROR);
        }
    }

    /**
     * 检查是否包含中文
     */
    public static void checkChineseWord(String str) {
        if (isContainChinese(str)) {
            throw new SysException(ExceptionEnum.CONTAINS_CHINESE);
        }
    }

    /**
     * 检查入参字符串是否为空
     */
    public static void checkStr(String str, String msg) {
        if (StringUtils.isBlank(str)) {
            throw new SysException(msg);
        }
    }

    /**
     * 检查浮点数
     */
    public static void checkDouble(Double value, String msg) {
        if (NumberUtils.isEmpty(value)) {
            throw new SysException(msg);
        }
    }

    /**
     * 检查入参是否为null
     */
    public static void checkNull(Object obj, String msg) {
        Optional.ofNullable(obj).orElseThrow(() -> new SysException(msg));
    }

    /**
     * 检查入参数组是否为空
     */
    public static void checkArray(Object[] array, String msg) {
        if (array == null || array.length == 0) {
            throw new SysException(msg);
        }
    }

    /**
     * 检查入参列表是否为空
     */
    public static void checkList(List list, String msg) {
        if (CollectionUtils.isEmpty(list)) {
            throw new SysException(msg);
        }
    }

    /**
     * 检查入参id是否为空
     */
    public static void checkLong(Long id, String msg) {
        if (id == null || id <= 0) {
            throw new SysException(msg);
        }
    }

    /**
     * 检查入参int是否为空
     */
    public static void checkInt(Integer value, String msg) {
        if (NumberUtils.isEmpty(value)) {
            throw new SysException(msg);
        }
    }

    /**
     * 检查int
     */
    public static  void checkInt(Integer value, int min, String msg) {
        if (value == null || value < min) {
            throw new SysException(msg);
        }
    }

    /**
     * 检查入参是否为空
     */
    public static void checkDecimal(BigDecimal value, String msg) {
        if (NumberUtils.isEmpty(value)) {
            throw new SysException(msg);
        }
    }
}
